package com.hp.docker_base.service.impl;


import com.alibaba.fastjson.JSON;
import com.hp.docker_base.algorithm.BRBInference4;
import com.hp.docker_base.bean.*;
import com.hp.docker_base.mapper.AnorectalMapper;
import com.hp.docker_base.util.ReadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AnorectalService {

    @Autowired
    ReadUtil readUtil;

    @Autowired
    AnorectalMapper anorectalMapper;

    private ArrayList<ArrayList<Double>> reference;
    private ArrayList<ArrayList<Double>> belief;

    List<DataInDto> dataInDtos;
    ArrayList<Double> input;
    ArrayList<ArrayList<Double>> Input;
    BRBInference4 brbInference4;

    public AnoreFidOutDto CalculateRes1(String RecId,
                                          String DeptId,
                                          String DataIn) {
        reference = null;
        belief = null;
        Map map = readUtil.Anore1_dataRead();
        reference = (ArrayList<ArrayList<Double>>) map.get("reference");
        belief = (ArrayList<ArrayList<Double>>) map.get("belief");

        dataInDtos = JSON.parseArray(DataIn, DataInDto.class);
        input = new ArrayList<>();
        for (int i = 0; i < dataInDtos.size(); i++) {
            input.add(Double.parseDouble(dataInDtos.get(i).getValue()));
        }
        ArrayList<ArrayList<Double>> Input = new ArrayList<>();
        Input.add(input);

        brbInference4 = new BRBInference4(reference, belief, Input);
        brbInference4.Init();
        brbInference4.Process();
        brbInference4.fusion();

        ArrayList<ArrayList<Double>> activation_weight = brbInference4.getActivation_weight();
        ArrayList<ArrayList<Double>> m_xin = brbInference4.getM_xin();

        List<ActivedRulesDto> ActivedRules = new ArrayList<>();
        for(int i=0; i<activation_weight.get(0).size(); i++){
            ActivedRulesDto temp = new ActivedRulesDto(i + 1, activation_weight.get(0).get(i));
            ActivedRules.add(temp);
        }

        List<DataOutDto> Result = new ArrayList<>();
        for(int i=0; i<m_xin.get(0).size(); i++){
            if(i<9){
                DataOutDto temp = new DataOutDto("sk0" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            } else {
                DataOutDto temp = new DataOutDto("sk" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            }
        }
        ArrayList<AnoreRule1> allData = anorectalMapper.getAllData1();
        AnoreFidOutDto anoreFidOutDto = new AnoreFidOutDto();
        anoreFidOutDto.setActivedRules(ActivedRules);
        anoreFidOutDto.setResult(Result);
        anoreFidOutDto.setDeptId(DeptId);
        anoreFidOutDto.setRecId(RecId);
        anoreFidOutDto.setAnoreRule1s(allData);
        return anoreFidOutDto;
    }

    public AnoreFidOutDto CalculateRes21(String RecId,
                                         String DeptId,
                                         String DataIn) {
        reference = null;
        belief = null;
        Map map = readUtil.Anore21_dataRead();
        reference = (ArrayList<ArrayList<Double>>) map.get("reference");
        belief = (ArrayList<ArrayList<Double>>) map.get("belief");

        dataInDtos = JSON.parseArray(DataIn, DataInDto.class);
        input = new ArrayList<>();
        for (int i = 0; i < dataInDtos.size(); i++) {
            input.add(Double.parseDouble(dataInDtos.get(i).getValue()));
        }
        ArrayList<ArrayList<Double>> Input = new ArrayList<>();
        Input.add(input);

        brbInference4 = new BRBInference4(reference, belief, Input);
        brbInference4.Init();
        brbInference4.Process();
        brbInference4.fusion();

        ArrayList<ArrayList<Double>> activation_weight = brbInference4.getActivation_weight();
        ArrayList<ArrayList<Double>> m_xin = brbInference4.getM_xin();

        List<ActivedRulesDto> ActivedRules = new ArrayList<>();
        for(int i=0; i<activation_weight.get(0).size(); i++){
            ActivedRulesDto temp = new ActivedRulesDto(i + 1, activation_weight.get(0).get(i));
            ActivedRules.add(temp);
        }

        List<DataOutDto> Result = new ArrayList<>();
        for(int i=0; i<m_xin.get(0).size(); i++){
            if(i<9){
                DataOutDto temp = new DataOutDto("sk0" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            } else {
                DataOutDto temp = new DataOutDto("sk" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            }
        }
        ArrayList<AnoreRule1> allData = anorectalMapper.getAllData1();
        AnoreFidOutDto anoreFidOutDto = new AnoreFidOutDto();
        anoreFidOutDto.setActivedRules(ActivedRules);
        anoreFidOutDto.setResult(Result);
        anoreFidOutDto.setDeptId(DeptId);
        anoreFidOutDto.setRecId(RecId);
        anoreFidOutDto.setAnoreRule1s(allData);
        return anoreFidOutDto;
    }

    public AnoreFidOutDto CalculateRes22(String RecId,
                                         String DeptId,
                                         String DataIn){
        dataInDtos = JSON.parseArray(DataIn, DataInDto.class);
        List<DataOutDto> Result = new ArrayList<>();
        if(dataInDtos.get(0).getValue().equals("0")){
            DataOutDto sk01 = new DataOutDto("sk01", 1);
            DataOutDto sk02 = new DataOutDto("sk02", 0);
            Result.add(sk01);
            Result.add(sk02);
        }else {
            DataOutDto sk01 = new DataOutDto("sk01", 0);
            DataOutDto sk02 = new DataOutDto("sk02", 1);
            Result.add(sk01);
            Result.add(sk02);
        }

        AnoreFidOutDto anoreFidOutDto = new AnoreFidOutDto();
        anoreFidOutDto.setResult(Result);
        anoreFidOutDto.setDeptId(DeptId);
        anoreFidOutDto.setRecId(RecId);
        return anoreFidOutDto;
    }

    public AnoreFidOutDto CalculateRes31(String RecId,
                                         String DeptId,
                                         String DataIn) {
        reference = null;
        belief = null;
        Map map = readUtil.Anore31_dataRead();
        reference = (ArrayList<ArrayList<Double>>) map.get("reference");
        belief = (ArrayList<ArrayList<Double>>) map.get("belief");

        dataInDtos = JSON.parseArray(DataIn, DataInDto.class);
        input = new ArrayList<>();
        for (int i = 0; i < dataInDtos.size(); i++) {
            input.add(Double.parseDouble(dataInDtos.get(i).getValue()));
        }
        ArrayList<ArrayList<Double>> Input = new ArrayList<>();
        Input.add(input);

        brbInference4 = new BRBInference4(reference, belief, Input);
        brbInference4.Init();
        brbInference4.Process();
        brbInference4.fusion();

        ArrayList<ArrayList<Double>> activation_weight = brbInference4.getActivation_weight();
        ArrayList<ArrayList<Double>> m_xin = brbInference4.getM_xin();

        List<ActivedRulesDto> ActivedRules = new ArrayList<>();
        for(int i=0; i<activation_weight.get(0).size(); i++){
            ActivedRulesDto temp = new ActivedRulesDto(i + 1, activation_weight.get(0).get(i));
            ActivedRules.add(temp);
        }

        List<DataOutDto> Result = new ArrayList<>();
        for(int i=0; i<m_xin.get(0).size(); i++){
            if(i<9){
                DataOutDto temp = new DataOutDto("sk0" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            } else {
                DataOutDto temp = new DataOutDto("sk" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            }
        }
        ArrayList<AnoreRule1> allData = anorectalMapper.getAllData1();
        AnoreFidOutDto anoreFidOutDto = new AnoreFidOutDto();
        anoreFidOutDto.setActivedRules(ActivedRules);
        anoreFidOutDto.setResult(Result);
        anoreFidOutDto.setDeptId(DeptId);
        anoreFidOutDto.setRecId(RecId);
        anoreFidOutDto.setAnoreRule1s(allData);
        return anoreFidOutDto;
    }

    public AnoreFidOutDto CalculateRes32(String RecId,
                                         String DeptId,
                                         String DataIn) {
        reference = null;
        belief = null;
        Map map = readUtil.Anore32_dataRead();
        reference = (ArrayList<ArrayList<Double>>) map.get("reference");
        belief = (ArrayList<ArrayList<Double>>) map.get("belief");

        dataInDtos = JSON.parseArray(DataIn, DataInDto.class);
        input = new ArrayList<>();
        for (int i = 0; i < dataInDtos.size(); i++) {
            input.add(Double.parseDouble(dataInDtos.get(i).getValue()));
        }
        ArrayList<ArrayList<Double>> Input = new ArrayList<>();
        Input.add(input);

        brbInference4 = new BRBInference4(reference, belief, Input);
        brbInference4.Init();
        brbInference4.Process();
        brbInference4.fusion();

        ArrayList<ArrayList<Double>> activation_weight = brbInference4.getActivation_weight();
        ArrayList<ArrayList<Double>> m_xin = brbInference4.getM_xin();

        List<ActivedRulesDto> ActivedRules = new ArrayList<>();
        for(int i=0; i<activation_weight.get(0).size(); i++){
            ActivedRulesDto temp = new ActivedRulesDto(i + 1, activation_weight.get(0).get(i));
            ActivedRules.add(temp);
        }

        List<DataOutDto> Result = new ArrayList<>();
        for(int i=0; i<m_xin.get(0).size(); i++){
            if(i<9){
                DataOutDto temp = new DataOutDto("sk0" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            } else {
                DataOutDto temp = new DataOutDto("sk" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            }
        }
        ArrayList<AnoreRule1> allData = anorectalMapper.getAllData1();
        AnoreFidOutDto anoreFidOutDto = new AnoreFidOutDto();
        anoreFidOutDto.setActivedRules(ActivedRules);
        anoreFidOutDto.setResult(Result);
        anoreFidOutDto.setDeptId(DeptId);
        anoreFidOutDto.setRecId(RecId);
        anoreFidOutDto.setAnoreRule1s(allData);
        return anoreFidOutDto;
    }

}
